//
//  OVELoginController.m
//  AddressBook Objective-C
//
//  Created by 王璐 on 2018/11/7.
//  Copyright © 2018 Ove. All rights reserved.
//

#import "OVELoginController.h"
#import "OVELoginView.h"
#import "OVEContactorController.h"
#import "MBProgressHUD.h"

#define USER @"wl"
#define PWD @"123"
#define REMBERPWD @"rember_pwd"
#define AUTOLOGIN @"auto_login"

@interface OVELoginController ()
@property (nonatomic,strong)OVELoginView *loginView;

@end

@implementation OVELoginController

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.loginView = [OVELoginView loginViewWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.loginView;
    self.view.backgroundColor = [UIColor yellowColor];
    [self.loginView.loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    //通知中心
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    //注册监听
    [center addObserver:self selector:@selector(textDidChange) name:UITextFieldTextDidChangeNotification object:self.loginView.userTF];
    [center addObserver:self selector:@selector(textDidChange) name:UITextFieldTextDidChangeNotification object:self.loginView.pwdTF];
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    //读取偏好设置信息
    self.loginView.userTF.text = [defaults objectForKey:USER];
    self.loginView.remberSwitch.on = [defaults boolForKey:REMBERPWD];
    if (self.loginView.remberSwitch.on) {
        self.loginView.pwdTF.text = [defaults objectForKey:PWD];
    }
    self.loginView.autoLoginSwitch.on = [defaults boolForKey:AUTOLOGIN];
    if (self.loginView.autoLoginSwitch.on ) {
        [self loginBtnClick];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"AddressBook";

}

#pragma mark - 通知中心监听事件
- (void)textDidChange {
    self.loginView.loginBtn.enabled = (self.loginView.userTF.text.length > 0 && self.loginView.pwdTF.text.length > 0);
}

-(void)loginBtnClick {
    NSLog(@"点击了登录按钮");
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.label.text = @"正在拼命加载中!!!";
    hud.removeFromSuperViewOnHide = YES;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        if ([self.loginView.userTF.text isEqualToString:@"wl"] && [self.loginView.pwdTF.text isEqualToString:@"123"]) {
            
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:self.loginView.userTF.text forKey:USER];
            [defaults setObject:self.loginView.pwdTF.text forKey:PWD];
            [defaults setBool:self.loginView.remberSwitch.isOn forKey:REMBERPWD];
            [defaults setBool:self.loginView.autoLoginSwitch.isOn forKey:AUTOLOGIN];
            [defaults synchronize];
            
            //登录成功
            OVEContactorController *contactorVC = [[OVEContactorController alloc] initWithStyle:UITableViewStyleGrouped];
            [self.navigationController pushViewController:contactorVC animated:YES];
            
            [hud hideAnimated:YES];
        }else{
            [hud hideAnimated:YES];
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            hud.mode = MBProgressHUDModeText;
            hud.label.text = @"账号或密码错误";
            hud.removeFromSuperViewOnHide = YES;
            [hud hideAnimated:YES afterDelay:1.0];
        }
        
        });

    
    
    
}




@end
